<?php

namespace app\admin\logic;

use app\common\model\Server;
use app\common\model\Work;
use think\Db;

/**
 * Class ServerLogic
 * @package app\admin\logic
 *
 * 客户管理
 * 客户登陆
 */
class ServerLogic
{
    function findOne($id){
    	return model('server')->find($id);
    }

    /*
     * 条件查询客户
     */
    function query($where)
    {
		return model('server')->where($where)->paginate(20);
    }

    /*
     * 新增客户
     */
    function add($data)
    {
    	if($data['id']>0){
		    return model('server')->data($data)->allowField(true)->isUpdate(true)->save();
	    }else{
    		return model('server')->data($data)->allowField(true)->save();
	    }
    }

    function del($id=0){
    	return model('server')->destroy($id);
    }

	// 根据工单id获取服务的项目数据
	public function getServerItems( $id=0 ) {
		$pageInfo=model('server')->where('work_id',$id)->paginate(20);
		$data['total'] = $pageInfo->lastPage();
		$data['page']  = $pageInfo->currentPage();
		$data['rows']  = $pageInfo->items();
    return $data;
	}
	//根据车牌号获取服务的历史数据，只要5条
	public function getServerHistory( $car_num='' ) {
    	// 根据车牌查找历史服务
		$data=model('work')
				->where('car_num',$car_num)
				// ->where('paid_time is not null')
				->order('date desc')
				->limit(1,5)
				->field('id,date,mileage')
				->select();
		// 根据工单id查找服务项目
		foreach ( $data as $k=>$v ) {
			// p($v);
			$serverItems=Db::query("select GROUP_CONCAT(name) as items from server where work_id=? limit 4",[$v['id']]);
			$v['serverItem']=$serverItems[0]['items'];
		}
		return $data;
	}



}
